import random
from PyQt5.QtCore import QThread,pyqtSignal

import time
from threading import Thread
from time import sleep,ctime
import cv2
from PIL import Image
from AI.yolo_CN_IC import YOLO_CN_IC
from AI.yolo_config import YOLO_CN_IC_CONFIG

class AI_Det():
    def __init__(self):
        self.classes = ['FOREIGN','SOLDER','BRIDGE','XIZHU']
        self.weights = './AI/weights/small_ng_weights.pth'
        self.yolo_CN_IC = YOLO_CN_IC(**YOLO_CN_IC_CONFIG)

    def run_image(self,image):
        img_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        image,ree = self.yolo_CN_IC.detect_image(img_pil)
        return image,ree


class AI_DetThread(QThread):
    startSig = pyqtSignal()
    finishSignal = pyqtSignal(list)
    def __init__(self,ai_model,parent=None):
        super(AI_DetThread,self).__init__(parent)
        self.AI_model = ai_model
        self.img_list = None
        # self.img_name = img_name
        print("loading AI_model------------------------------")

    def insert_images(self,args):
        self.args = args
        if len(self.args)<=0:
            return
        self.image_name = '{}_{}.jpg'.format(args[0], args[1])
    def run(self):
        self.startSig.emit()
        print(self.args)
        if len(self.args)<=0:
            return
        sleep(1)
        # w,h = img.shape[0],img.shape[1]
        result = random.sample(['22','23'],1)
        self.args[2] = result[0]
        self.finishSignal.emit(self.args)
    # def start_thread(self,img_name):
    #     t1 = MyThread(self.run_ai, (img_name))
    #     t1.start()
    #     # t1.join()
    #     res = t1.getResult()
    #     return res
    # def run_ai(self,img_name):
    #     # img = cv2.imread(img_name)
    #     # img_
    #     print(img_name)
    #     sleep(1)
    #     # w,h = img.shape[0],img.shape[1]
    #     result = random.sample(['22','23'],1)
    #     return result

# class MyThread(Thread):
#     def __init__(self, func, img_name):
#         '''
#         :param func: 可调用的对象
#         :param args: 可调用对象的参数
#         '''
#         Thread.__init__(self)   # 不要忘记调用Thread的初始化方法
#         self.func = func
#         self.args = img_name
#         self.result = None
#     def run(self):
#         self.result=self.func(self.args)
#
#     def getResult(self):
#         return self.result
#
#
# def func(name, sec):
#     print('---开始---', name, '时间', ctime())
#     sleep(sec)
#     print('***结束***', name, '时间', ctime())
